package com.yhsoft.buyer.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yhsoft.bill.controller.request.SysYckLogInVo;
import com.yhsoft.bill.controller.response.SysXhLogOutVo;
import com.yhsoft.bill.service.IVenderYszkMxService;
import com.yhsoft.buyer.controller.request.VenderOrdersInVo;
import com.yhsoft.buyer.controller.response.VenderOrdersOutVo;
import com.yhsoft.buyer.mapper.OrderReportMapper;
import com.yhsoft.buyer.service.IBuyerOrderService;
import com.yhsoft.buyer.service.OrderReportService;
import com.yhsoft.common.service.TokenService;
import io.swagger.annotations.ApiModelProperty;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class OrderReportServiceImpl implements OrderReportService {
    @Resource
    private OrderReportMapper orderReportMapper;

    @Resource
    private IBuyerOrderService iBuyerOrderService;

    @Resource
    private IVenderYszkMxService iVenderYszkMxService;

    @Override
    public void OrderListReport(HttpServletResponse response, Integer vendorId) throws IOException {
        try {
            // 设置文件名
            IPage<VenderOrdersOutVo> iPage = iBuyerOrderService.getOrderList(new VenderOrdersInVo(), 1L, 10000000L);

            //List<VenderOrdersInVo> logData= orderReportMapper.selectByVendorId(VenderOrdersInVo.class,vendorId);
            List<VenderOrdersOutVo> logData = iPage.getRecords();
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("订单列表报表");
            //创立标题行
            XSSFRow headerRow = sheet.createRow(0);
            String[] headers = {"供应商订单号", "商品名称", "商品类型", "型号", "数量", "金额", "收货人", "联系手机号", "收货地址", "支付时间", "快递公司", "快递单号", "订单状态"};
            for (int i = 0; i < headers.length; i++) {
                Cell cell = headerRow.createCell(i);
                cell.setCellValue(headers[i]);
            }
            //填充行数据
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            for (int i = 0; i < logData.size(); i++) {
                VenderOrdersOutVo venderOrdersOutVo = logData.get(i);
                Row row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(venderOrdersOutVo.getOrderNo());
                row.createCell(1).setCellValue(venderOrdersOutVo.getGoodsName());
                row.createCell(2).setCellValue("线下供应商");
                row.createCell(3).setCellValue(venderOrdersOutVo.getSkuName());
                row.createCell(4).setCellValue(venderOrdersOutVo.getSkuQuantity().shortValue());
                row.createCell(5).setCellValue(venderOrdersOutVo.getTotlePrice().floatValue());
                row.createCell(6).setCellValue(venderOrdersOutVo.getName());
                row.createCell(7).setCellValue(venderOrdersOutVo.getMobile());
                row.createCell(8).setCellValue(venderOrdersOutVo.getAddress());
                if (venderOrdersOutVo.getPayTime() != null) {
                    row.createCell(9).setCellValue(formatter.format(venderOrdersOutVo.getPayTime()));
                } else {
                    row.createCell(9).setCellValue("");
                }
                row.createCell(10).setCellValue(venderOrdersOutVo.getExpressCompany());
                row.createCell(11).setCellValue(venderOrdersOutVo.getExpressNo());
                row.createCell(12).setCellValue(getOrderStutas(venderOrdersOutVo.getOrderStatus()));
            }
            //调整列宽
            for (int i = 0; i < headers.length; i++) {
                sheet.autoSizeColumn(i);
            }
            //保存Excel文件
            //通过输出流将excel文件下载到客户端浏览器
            // 设置响应头
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("订单列表", "UTF-8") + ".xlsx");
            ServletOutputStream out = response.getOutputStream();
            workbook.write(out);

            //关闭资源
            out.close();
            workbook.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        System.out.println("导出文件成功！");
    }


    //供货商消耗日志对账表
    @Override
    public void LogReport(HttpServletResponse response) throws IOException {
        try {
            IPage<SysXhLogOutVo> iPage = iVenderYszkMxService.getXhLog(new SysYckLogInVo(), 1L, 10000000L);
            //List<VenderOrdersOutVo> logData= orderReportMapper.selectAu(VenderOrdersOutVo.class);
            List<SysXhLogOutVo> logData = iPage.getRecords();
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("消耗日志对账表");
            //创立标题行
            XSSFRow headerRow = sheet.createRow(0);
            String[] headers = {"供应商订单号", "商品名称", "商品类型", "型号", "数量", "金额", "收货人", "联系手机号", "支付时间", "快递公司", "快递单号"};
            for (int i = 0; i < headers.length; i++) {
                Cell cell = headerRow.createCell(i);
                cell.setCellValue(headers[i]);
            }
            //填充行数据
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            for (int i = 0; i < logData.size(); i++) {
                SysXhLogOutVo venderOrdersOutVo = logData.get(i);
                Row row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(venderOrdersOutVo.getOrderNo());
                row.createCell(1).setCellValue(venderOrdersOutVo.getGoodsName());
                row.createCell(2).setCellValue("线下供应商");
                row.createCell(3).setCellValue(venderOrdersOutVo.getSkuName());
                row.createCell(4).setCellValue(venderOrdersOutVo.getSkuQuantity().shortValue());
                row.createCell(5).setCellValue(venderOrdersOutVo.getTotlePrice().floatValue());
                row.createCell(6).setCellValue(venderOrdersOutVo.getName());
                row.createCell(7).setCellValue(venderOrdersOutVo.getMobile());
                if (venderOrdersOutVo.getPayTime() != null) {
                    row.createCell(8).setCellValue(formatter.format(venderOrdersOutVo.getPayTime()));
                } else {
                    row.createCell(8).setCellValue("");
                }
                row.createCell(9).setCellValue(venderOrdersOutVo.getExpressCompany());
                row.createCell(10).setCellValue(venderOrdersOutVo.getExpressNo());
            }
            //调整列宽
            for (int i = 0; i < headers.length; i++) {
                sheet.autoSizeColumn(i);
            }
            // 设置响应头
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("消耗日志对账表", "UTF-8") + ".xlsx");
            //保存Excel文件
            //通过输出流将excel文件下载到客户端浏览器
            ServletOutputStream out = response.getOutputStream();
            workbook.write(out);

            //关闭资源
            out.close();
            workbook.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("导出文件成功！");
    }

    private String getOrderStutas(Integer orderStatus) {
        // 订单状态 0-已完成 1-待支付 2-待发货 3-待收货 4-已取消 5-支付中 6-支付失败 7-已退款
        switch (orderStatus) {
            case 0:
                return "已完成";
            case 1:
                return "待支付";
            case 2:
                return "待发货";
            case 3:
                return "待收货";
            case 4:
                return "已取消";
            case 5:
                return "支付中";
            case 6:
                return "支付失败";
            case 7:
                return "已退款";
            default:
                return "未知状态";
        }
    }
}

